home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / EnhancedMesh / EnhancedMesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  20.5 KB  |  592 lines

  1. //-----------------------------------------------------------------------------
  2. // File: EnhancedMesh.cpp
  3. //
  4. // Desc: Sample showing enhanced meshes in D3D
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <commdlg.h>
  11. #include <tchar.h>
  12. #include <stdio.h>
  13. #include <d3dx8.h>
  14. #include "D3DApp.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18. #include "resource.h"
  19.  
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // Name: class CMyD3DApplication
  24. // Desc: Application class. The base class (CD3DApplication) provides the 
  25. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  26. //       adds functionality specific to this sample program.
  27. //-----------------------------------------------------------------------------
  28. class CMyD3DApplication : public CD3DApplication
  29. {
  30.     TCHAR               m_strMeshFilename[512];
  31.     TCHAR               m_strInitialDir[512];
  32.  
  33.     LPD3DXMESH          m_pMeshSysMem;      // system memory version of mesh, lives through resize's
  34.     LPD3DXMESH          m_pMeshEnhanced;    // vid mem version of mesh that is enhanced
  35.     UINT                m_dwNumSegs;        // number of segments per edge (tesselation level)
  36.     D3DXMATERIAL*       m_pMaterials;       // pointer to material info in m_pbufMaterials
  37.     LPDIRECT3DTEXTURE8* m_ppTextures;       // array of textures, entries are NULL if no texture specified
  38.     DWORD               m_dwNumMaterials;   // number of materials
  39.  
  40.     CD3DFont*           m_pFont;
  41.     CD3DArcBall         m_ArcBall;          // mouse rotation utility
  42.     D3DXVECTOR3         m_vObjectCenter;    // Center of bounding sphere of object
  43.     FLOAT               m_fObjectRadius;    // Radius of bounding sphere of object
  44.  
  45.     LPD3DXBUFFER        m_pbufMaterials;    // contains both the materials data and the filename strings
  46.     LPD3DXBUFFER        m_pbufAdjacency;    // Contains the adjacency info loaded with the mesh
  47.  
  48.     BOOL                m_bUseHWNPatches; 
  49.  
  50.     HRESULT GenerateEnhancedMesh(UINT cNewNumSegs);
  51.  
  52. protected:
  53.     HRESULT OneTimeSceneInit();
  54.     HRESULT InitDeviceObjects();
  55.     HRESULT ConfirmDevice (D3DCAPS8* pCaps, DWORD dwBehaviorFlags, D3DFORMAT fmt);
  56.     HRESULT RestoreDeviceObjects();
  57.     HRESULT InvalidateDeviceObjects();
  58.     HRESULT DeleteDeviceObjects();
  59.     HRESULT Render();
  60.     HRESULT FrameMove();
  61.     HRESULT FinalCleanup();
  62.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  63.  
  64. public:
  65.     CMyD3DApplication();
  66. };
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: WinMain()
  73. // Desc: Entry point to the program. Initializes everything, and goes into a
  74. //       message-processing loop. Idle time is used to render the scene.
  75. //-----------------------------------------------------------------------------
  76. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  77. {
  78.     CMyD3DApplication d3dApp;
  79.  
  80.     if( FAILED( d3dApp.Create( hInst ) ) )
  81.         return 0;
  82.     return d3dApp.Run();
  83. }
  84.  
  85.  
  86.  
  87.  
  88. //-----------------------------------------------------------------------------
  89. // Name: CMyD3DApplication()
  90. // Desc: Application constructor. Sets attributes for the app.
  91. //-----------------------------------------------------------------------------
  92. CMyD3DApplication::CMyD3DApplication()
  93. {
  94.     m_strWindowTitle    = _T("Enhanced Mesh - N-Patches");
  95.     m_bUseDepthBuffer   = TRUE;
  96.     m_bShowCursorWhenFullscreen = TRUE;
  97.  
  98.     m_pMeshSysMem     = NULL;
  99.     m_pMeshEnhanced   = NULL;
  100.     m_pMaterials      = NULL;
  101.     m_ppTextures      = NULL;
  102.     m_dwNumMaterials  = NULL;
  103.     m_pbufMaterials   = NULL;
  104.     m_pbufAdjacency   = NULL;
  105.     m_dwNumSegs       = 2;
  106.     m_bUseHWNPatches  = FALSE;
  107.     
  108.     m_pFont           = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  109.  
  110.     _tcscpy( m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
  111.     _tcscpy( m_strMeshFilename, _T("tiger.x") );
  112. }
  113.  
  114.  
  115.  
  116.  
  117. //-----------------------------------------------------------------------------
  118. // Name: OneTimeSceneInit()
  119. // Desc: Called during initial app startup, this function performs all the
  120. //       permanent initialization.
  121. //-----------------------------------------------------------------------------
  122. HRESULT CMyD3DApplication::OneTimeSceneInit()
  123. {
  124.     // Set cursor to indicate that user can move the object with the mouse
  125. #ifdef _WIN64
  126.     SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
  127. #else
  128.     SetClassLong( m_hWnd, GCL_HCURSOR, (LONG)LoadCursor( NULL, IDC_SIZEALL ) );
  129. #endif
  130.     return S_OK;
  131. }
  132.  
  133.  
  134.  
  135.  
  136. //-----------------------------------------------------------------------------
  137. // Name: FrameMove()
  138. // Desc: Called once per frame, the call is the entry point for animating
  139. //       the scene.
  140. //-----------------------------------------------------------------------------
  141. HRESULT CMyD3DApplication::FrameMove()
  142. {
  143.      // Setup world matrix
  144.     D3DXMATRIX matWorld;
  145.     D3DXMatrixTranslation( &matWorld, -m_vObjectCenter.x,
  146.                                       -m_vObjectCenter.y,
  147.                                       -m_vObjectCenter.z );
  148.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetRotationMatrix() );
  149.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetTranslationMatrix() );
  150.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  151.  
  152.     D3DXMATRIX matView;
  153.     D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0, 0, -2*m_fObjectRadius ),
  154.                                   &D3DXVECTOR3( 0, 0, 0 ),
  155.                                   &D3DXVECTOR3( 0, 1, 0 ) );
  156.     m_pd3dDevice->SetTransform( D3DTS_VIEW,  &matView );
  157.  
  158.     return S_OK;
  159. }
  160.  
  161.  
  162.  
  163.  
  164. //-----------------------------------------------------------------------------
  165. // Name: Render()
  166. // Desc: Called once per frame, the call is the entry point for 3d
  167. //       rendering. This function sets up render states, clears the
  168. //       viewport, and renders the scene.
  169. //-----------------------------------------------------------------------------
  170. HRESULT CMyD3DApplication::Render()
  171. {
  172.     // Clear the backbuffer
  173.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
  174.                          0x000000ff, 1.0f, 0L );
  175.  
  176.     // Begin the scene
  177.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  178.     {
  179.         if (m_bUseHWNPatches)
  180.         {
  181.             float fNumSegs;
  182.  
  183.             fNumSegs = (float)m_dwNumSegs;
  184.             m_pd3dDevice->SetRenderState(D3DRS_PATCHSEGMENTS, *((DWORD*)&fNumSegs));
  185.         }
  186.  
  187.         // set and draw each of the materials in the mesh
  188.         for( UINT i = 0; i < m_dwNumMaterials; i++ )
  189.         {
  190.             m_pd3dDevice->SetMaterial( &m_pMaterials[i].MatD3D );
  191.             m_pd3dDevice->SetTexture( 0, m_ppTextures[i] );
  192.  
  193.             m_pMeshEnhanced->DrawSubset( i );
  194.         }
  195.  
  196.         if (m_bUseHWNPatches)
  197.         {
  198.             m_pd3dDevice->SetRenderState(D3DRS_PATCHSEGMENTS, 0);
  199.         }
  200.  
  201.         // Output stats
  202.         {
  203.             // Output statistics
  204.             m_pFont->DrawText(   2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  205.             m_pFont->DrawText(   2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  206.  
  207.             TCHAR buffer1[80], buffer2[80];
  208.             _stprintf( buffer1, "NumSegs:" ); 
  209.             _stprintf( buffer2, "%d ", m_dwNumSegs );
  210.             m_pFont->DrawText(   2, 40, 0xffffff00, buffer1 );
  211.             m_pFont->DrawText( 150, 40, 0xffffffff, buffer2 );
  212.             _stprintf( buffer1, "NumFaces:" );
  213.             _stprintf( buffer2, "%d ", (m_pMeshEnhanced == NULL) ? 0 : m_pMeshEnhanced->GetNumFaces() );
  214.             m_pFont->DrawText(   2, 60, 0xffffff00, buffer1 );
  215.             m_pFont->DrawText( 150, 60, 0xffffffff, buffer2 );
  216.             _stprintf( buffer1, "NumVertices:" );
  217.             _stprintf( buffer2, "%d ", (m_pMeshEnhanced == NULL) ? 0 : m_pMeshEnhanced->GetNumVertices() );
  218.             m_pFont->DrawText(   2, 80, 0xffffff00, buffer1 );
  219.             m_pFont->DrawText( 150, 80, 0xffffffff, buffer2 );
  220.         }
  221.  
  222.         // End the scene.
  223.         m_pd3dDevice->EndScene();
  224.     }
  225.  
  226.     return S_OK;
  227. }
  228.  
  229.  
  230.  
  231.  
  232. //-----------------------------------------------------------------------------
  233. // Name: InitDeviceObjects()
  234. // Desc: This creates all device-dependent managed objects, such as managed
  235. //       textures and managed vertex buffers.
  236. //-----------------------------------------------------------------------------
  237. HRESULT CMyD3DApplication::InitDeviceObjects()
  238. {
  239.     LPDIRECT3DVERTEXBUFFER8 pVB = NULL;
  240.     BYTE*      pVertices = NULL;
  241.     LPD3DXMESH pTempMesh;
  242.     TCHAR      strMeshPath[512];
  243.     HRESULT    hr;
  244.  
  245.     // Initialize the font's internal textures
  246.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  247.  
  248.     // Load the mesh from the specified file
  249.     DXUtil_FindMediaFile( strMeshPath, m_strMeshFilename );
  250.  
  251.     hr = D3DXLoadMeshFromX( strMeshPath, D3DXMESH_SYSTEMMEM, m_pd3dDevice, 
  252.                             &m_pbufAdjacency, &m_pbufMaterials, &m_dwNumMaterials, 
  253.                             &m_pMeshSysMem );
  254.     if( FAILED(hr) )
  255.         // hide error so that we can display a blue screen
  256.         return S_OK;
  257.  
  258.     // Lock the vertex buffer, to generate a simple bounding sphere
  259.     hr = m_pMeshSysMem->GetVertexBuffer( &pVB );
  260.     if( FAILED(hr) )
  261.         return hr;
  262.  
  263.     hr = pVB->Lock( 0, 0, &pVertices, 0 );
  264.     if( FAILED(hr) )
  265.     {
  266.         SAFE_RELEASE( pVB );
  267.         return hr;
  268.     }
  269.  
  270.     hr = D3DXComputeBoundingSphere( pVertices, m_pMeshSysMem->GetNumVertices(), 
  271.                                     m_pMeshSysMem->GetFVF(), &m_vObjectCenter, 
  272.                                     &m_fObjectRadius );
  273.     if( FAILED(hr) )
  274.     {
  275.         pVB->Unlock();
  276.         SAFE_RELEASE( pVB );
  277.         return hr;
  278.     }
  279.  
  280.     if( 0 == m_dwNumMaterials )
  281.     {
  282.         pVB->Unlock();
  283.         SAFE_RELEASE( pVB );
  284.         return E_INVALIDARG;
  285.     }
  286.  
  287.     // Get the array of materials out of the returned buffer, allocate a
  288.     // texture array, and load the textures
  289.     m_pMaterials = (D3DXMATERIAL*)m_pbufMaterials->GetBufferPointer();
  290.     m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
  291.  
  292.     for( UINT i=0; i<m_dwNumMaterials; i++ )
  293.     {
  294.         TCHAR strTexturePath[512] = _T("");
  295.         DXUtil_FindMediaFile( strTexturePath, m_pMaterials[i].pTextureFilename );
  296.         if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, strTexturePath, 
  297.                                                &m_ppTextures[i] ) ) )
  298.             m_ppTextures[i] = NULL;
  299.     }
  300.  
  301.     pVB->Unlock();
  302.     SAFE_RELEASE( pVB );
  303.  
  304.     // Make sure there are normals, which are required for the tesselation
  305.     // enhancement
  306.     if( !(m_pMeshSysMem->GetFVF() & D3DFVF_NORMAL) )
  307.     {
  308.         hr = m_pMeshSysMem->CloneMeshFVF( m_pMeshSysMem->GetOptions(), 
  309.                                           m_pMeshSysMem->GetFVF() | D3DFVF_NORMAL, 
  310.                                           m_pd3dDevice, &pTempMesh );
  311.         if( FAILED(hr) )
  312.             return hr;
  313.  
  314.         D3DXComputeNormals( pTempMesh, NULL );
  315.  
  316.         SAFE_RELEASE( m_pMeshSysMem );
  317.         m_pMeshSysMem = pTempMesh;
  318.     }
  319.  
  320.     return S_OK;
  321. }
  322.  
  323.  
  324.  
  325.  
  326. //-----------------------------------------------------------------------------
  327. // Name: GenerateEnhancedMesh()
  328. // Desc: 
  329. //-----------------------------------------------------------------------------
  330. HRESULT CMyD3DApplication::GenerateEnhancedMesh( UINT dwNewNumSegs )
  331. {
  332.     LPD3DXMESH pMeshEnhancedSysMem = NULL;
  333.     LPD3DXMESH pMeshTemp;
  334.     HRESULT    hr;
  335.  
  336.     if (m_pMeshSysMem == NULL)
  337.         return S_OK;
  338.  
  339.     // if using hw, just copy the mesh
  340.     if (m_bUseHWNPatches)
  341.     {
  342.         hr = m_pMeshSysMem->CloneMeshFVF( D3DXMESH_WRITEONLY | D3DXMESH_NPATCHES | 
  343.             (m_pMeshSysMem->GetOptions() & D3DXMESH_32BIT), 
  344.             m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
  345.         if( FAILED(hr) )
  346.             return hr;
  347.     }
  348.     else  // tesselate the mesh in sw
  349.     {
  350.  
  351.         // Create an enhanced version of the mesh, will be in sysmem since source is
  352.         hr = D3DXTessellateNPatches( m_pMeshSysMem, (DWORD*)m_pbufAdjacency->GetBufferPointer(), 
  353.                                      (float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem, NULL );
  354.         if( FAILED(hr) )
  355.         {
  356.             // If the tessellate failed, there might have been more triangles or vertices 
  357.             // than can fit into a 16bit mesh, so try cloning to 32bit before tessellation
  358.  
  359.             hr = m_pMeshSysMem->CloneMeshFVF( D3DXMESH_SYSTEMMEM | D3DXMESH_32BIT, 
  360.                 m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
  361.             if (FAILED(hr))
  362.                 return hr;
  363.  
  364.             hr = D3DXTessellateNPatches( pMeshTemp, (DWORD*)m_pbufAdjacency->GetBufferPointer(), 
  365.                                          (float)dwNewNumSegs, FALSE, &pMeshEnhancedSysMem, NULL );
  366.             if( FAILED(hr) )
  367.             {
  368.                 pMeshTemp->Release();
  369.                 return hr;
  370.             }
  371.  
  372.             pMeshTemp->Release();
  373.         }
  374.  
  375.         // Make a vid mem version of the mesh  
  376.         // Only set WRITEONLY if it doesn't use 32bit indices, because those 
  377.         // often need to be emulated, which means that D3DX needs read-access.
  378.         DWORD dwMeshEnhancedFlags = pMeshEnhancedSysMem->GetOptions() & D3DXMESH_32BIT;
  379.         if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
  380.             dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
  381.         hr = pMeshEnhancedSysMem->CloneMeshFVF( dwMeshEnhancedFlags, m_pMeshSysMem->GetFVF(),
  382.                                                 m_pd3dDevice, &pMeshTemp );
  383.         if( FAILED(hr) )
  384.         {
  385.             SAFE_RELEASE( pMeshEnhancedSysMem );
  386.             return hr;
  387.         }
  388.  
  389.         // Latch in the enhanced mesh
  390.         SAFE_RELEASE( pMeshEnhancedSysMem );
  391.     }
  392.  
  393.     SAFE_RELEASE( m_pMeshEnhanced );
  394.     m_pMeshEnhanced = pMeshTemp;
  395.     m_dwNumSegs     = dwNewNumSegs;
  396.  
  397.     return S_OK;
  398. }
  399.  
  400.  
  401.  
  402.  
  403. //-----------------------------------------------------------------------------
  404. // Name: RestoreDeviceObjects()
  405. // Desc: Restore device-memory objects and state after a device is created or
  406. //       resized.
  407. //-----------------------------------------------------------------------------
  408. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  409. {
  410.     HRESULT hr;
  411.  
  412.     m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);
  413.  
  414.     hr = GenerateEnhancedMesh( m_dwNumSegs );
  415.     if( FAILED(hr) )
  416.         return hr;
  417.  
  418.     // Setup render state
  419.     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
  420.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
  421.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
  422.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,      0x33333333 );
  423.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  424.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  425.  
  426.     // Setup the light
  427.     D3DLIGHT8 light;
  428.     D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
  429.     m_pd3dDevice->SetLight(0, &light );
  430.     m_pd3dDevice->LightEnable(0, TRUE );
  431.  
  432.     // Setup the arcball parameters
  433.     m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 0.85f );
  434.     m_ArcBall.SetRadius( 1.0f );
  435.  
  436.     // Setup the projection matrix
  437.     D3DXMATRIX matProj;
  438.     FLOAT      fAspect = (FLOAT)m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  439.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 
  440.                                 m_fObjectRadius/64.0f, m_fObjectRadius*200.0f );
  441.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  442.  
  443.     // Restore the font
  444.     m_pFont->RestoreDeviceObjects();
  445.  
  446.     return S_OK;
  447. }
  448.  
  449.  
  450.  
  451.  
  452. //-----------------------------------------------------------------------------
  453. // Name: InvalidateDeviceObjects()
  454. // Desc: Called when the device-dependent objects are about to be lost.
  455. //-----------------------------------------------------------------------------
  456. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  457. {
  458.     m_pFont->InvalidateDeviceObjects();
  459.  
  460.     SAFE_RELEASE( m_pMeshEnhanced );
  461.  
  462.     return S_OK;
  463. }
  464.  
  465.  
  466.  
  467.  
  468. //-----------------------------------------------------------------------------
  469. // Name: DeleteDeviceObjects()
  470. // Desc: Called when the app is exiting, or the device is being changed,
  471. //       this function deletes any device dependent objects.
  472. //-----------------------------------------------------------------------------
  473. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  474. {
  475.     m_pFont->DeleteDeviceObjects();
  476.  
  477.     for( UINT i = 0; i < m_dwNumMaterials; i++ )
  478.         SAFE_RELEASE( m_ppTextures[i] );
  479.     SAFE_DELETE_ARRAY( m_ppTextures );
  480.     SAFE_RELEASE( m_pMeshSysMem );
  481.     SAFE_RELEASE( m_pbufMaterials );
  482.     m_dwNumMaterials = 0L;
  483.  
  484.     return S_OK;
  485. }
  486.  
  487.  
  488.  
  489.  
  490.  
  491. //-----------------------------------------------------------------------------
  492. // Name: FinalCleanup()
  493. // Desc: Called during initial app startup, this function performs all the
  494. //       permanent initialization.
  495. //-----------------------------------------------------------------------------
  496. HRESULT CMyD3DApplication::FinalCleanup()
  497. {
  498.     SAFE_DELETE( m_pFont );
  499.  
  500.     return S_OK;
  501. }
  502.  
  503.  
  504.  
  505.  
  506. //-----------------------------------------------------------------------------
  507. // Name: MsgProc()
  508. // Desc: Message proc function to handle key and menu input
  509. //-----------------------------------------------------------------------------
  510. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  511.                                     LPARAM lParam )
  512. {
  513.     // Pass mouse messages to the ArcBall so it can build internal matrices
  514.     m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
  515.  
  516.     // Trap context menu
  517.     if( WM_CONTEXTMENU == uMsg )
  518.         return 0;
  519.  
  520.     // Handle key presses
  521.     if( WM_KEYDOWN == uMsg )
  522.     {
  523.         if( VK_UP == (int)wParam )
  524.             GenerateEnhancedMesh( m_dwNumSegs + 1 );
  525.         
  526.         if( VK_DOWN == (int)wParam )
  527.             GenerateEnhancedMesh( max( 1, m_dwNumSegs - 1 ) );
  528.  
  529.         if( 'W' == (int)wParam )
  530.             m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
  531.  
  532.         if( 'S' == (int)wParam )
  533.             m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
  534.     }
  535.     else if( uMsg == WM_COMMAND )
  536.     {
  537.             // Handle the open file command
  538.         if( LOWORD(wParam) == IDM_OPENFILE )
  539.         {
  540.             TCHAR g_strFilename[512]   = _T("");
  541.  
  542.             // Display the OpenFileName dialog. Then, try to load the specified file
  543.             OPENFILENAME ofn = { sizeof(OPENFILENAME), NULL, NULL,
  544.                                 _T(".X Files (.x)\0*.x\0\0"), 
  545.                                 NULL, 0, 1, m_strMeshFilename, 512, g_strFilename, 512, 
  546.                                 m_strInitialDir, _T("Open Mesh File"), 
  547.                                 OFN_FILEMUSTEXIST, 0, 1, NULL, 0, NULL, NULL };
  548.  
  549.             if( TRUE == GetOpenFileName( &ofn ) )
  550.             {
  551.                 _tcscpy( m_strInitialDir, m_strMeshFilename );
  552.                 TCHAR* pLastSlash =  _tcsrchr( m_strInitialDir, _T('\\') );
  553.                 if( pLastSlash )
  554.                     *pLastSlash = 0;
  555.                 SetCurrentDirectory( m_strInitialDir );
  556.  
  557.                 // Destroy and recreate everything
  558.                 InvalidateDeviceObjects();
  559.                 DeleteDeviceObjects();
  560.                 InitDeviceObjects();
  561.                 RestoreDeviceObjects();
  562.             }
  563.         }
  564.     }
  565.  
  566.  
  567.     // Pass remaining messages to default handler
  568.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  569. }
  570.  
  571. //-----------------------------------------------------------------------------
  572. // Name: ConfirmDevice()
  573. // Desc: Called during device intialization, this code checks the device
  574. //       for some minimum set of capabilities
  575. //-----------------------------------------------------------------------------
  576. HRESULT CMyD3DApplication::ConfirmDevice (D3DCAPS8* pCaps, DWORD dwBehavior, 
  577.                                           D3DFORMAT fmt)
  578. {
  579.     // On a non-pure device, if it can do rt-Patches, 
  580.     // it can do n-Patches as well.
  581.     if ((dwBehavior & D3DCREATE_PUREDEVICE) && 
  582.         ((pCaps->DevCaps & D3DDEVCAPS_NPATCHES) == 0) && 
  583.         (pCaps->DevCaps & D3DDEVCAPS_RTPATCHES))
  584.         return E_FAIL;
  585.         
  586.     return S_OK;
  587. }
  588.  
  589.  
  590.  
  591.  
  592.